// Copyright 2016 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build example
// +build example

package main

import (
	"bytes"
	"fmt"
	"image"
	"image/color"
	_ "image/png"
	"log"
	"os"

	"gitee.com/yysama/Kindling_civilization/core/assetloader"
	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/text"
	"golang.org/x/image/font"
)

const (
	screenWidth  = 320
	screenHeight = 240
)

var (
	bgImg    *ebiten.Image
	txtImg   = ebiten.NewImage(100, 100)
	fontface font.Face
)

func init() {
	buffer, _ := os.ReadFile("../asset/images/loading/bg.png")
	img, _, _ := image.Decode(bytes.NewBuffer(buffer))
	bgImg = ebiten.NewImageFromImage(img)
	fontface, _ = assetloader.NewFont("../asset/font/w4.ttf", 12, 72)
}

type Game struct {
	txt string
}

func (g *Game) Update() error {
	g.txt = fmt.Sprintf("TPS: %0.2f\n", ebiten.CurrentTPS())
	return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
	//方案1、可以
	//ebitenutil.DebugPrint(screen, g.txt)

	//方案2、带重影
	w, h := bgImg.Size()
	tmpBg := ebiten.NewImage(w, h)
	defer tmpBg.Dispose()
	tmpBg.DrawImage(bgImg, &ebiten.DrawImageOptions{})
	text.Draw(tmpBg, g.txt, fontface, 30, 40, color.White)
	screen.DrawImage(tmpBg, &ebiten.DrawImageOptions{})
}

func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
	return screenWidth, screenHeight
}

func main() {
	ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
	ebiten.SetWindowTitle("Infinite Scroll (Ebiten Demo)")
	ebiten.SetMaxTPS(60)
	if err := ebiten.RunGame(&Game{}); err != nil {
		log.Fatal(err)
	}
}
